package com.qfedu.video.controller;


import com.github.pagehelper.PageInfo;
import com.qfedu.video.entry.Course;
import com.qfedu.video.entry.Speaker;
import com.qfedu.video.entry.Video;
import com.qfedu.video.entry.VideoVo;
import com.qfedu.video.service.CourseService;
import com.qfedu.video.service.SpeakerService;
import com.qfedu.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    VideoService videoService;
    @Autowired
    SpeakerService speakerService;

    @Autowired
    CourseService courseService;

    @RequestMapping("/showList")
    public String showList(Model model, VideoVo videoVo){

        System.out.println(videoVo);
        // ctrl + y
        List<Video> videos =videoService.getVideos(videoVo);
        PageInfo<Video> pageInfo = new PageInfo<Video>(videos);
        //pageInfo.setPages(10);
        //pageInfo.setPageSize(videoVo.getSize());
        model.addAttribute("pageInfo",pageInfo);
        List<Speaker> speakerList= speakerService.showAllSpeaker();
        System.out.println(speakerList);
        model.addAttribute("speakerList",speakerList);
        return "behind/videoList";
    }

    @RequestMapping("/delBatchVideos")
    public String delBatchVideos(Integer[] ids){
        System.out.println(Arrays.toString(ids));

        //开始真正的删除数据
       int i= videoService.deleteBatchByIds(ids);

       // 删除成功后，我希望进入到一个 带有数据的list页面
        // 如果直接跳转到页面，没有数据，所以我希望跳转到一个请求中
        // 跳转到请求中，但是会走视图解析器，于是乎，使用重定向或者内部转发
        // 因为内部转发url不发生变化，所以使用了重定向
        return "redirect:/video/showList";
    }

    @RequestMapping("/edit/{sid}")
    public String edit(@PathVariable("sid") int id,Model model){

        System.out.println(id);
       Video video= videoService.getVideoById(id);
        model.addAttribute("video",video);

        List<Speaker> speakerList= speakerService.showAllSpeaker();
        System.out.println(speakerList);
        model.addAttribute("speakerList",speakerList);

        return "behind/addVideo";
    }


    @RequestMapping("/showAdd")
    public String showAdd(Model model){
        List<Speaker> speakerList= speakerService.showAllSpeaker();
        model.addAttribute("speakerList",speakerList);
        return "behind/addVideo";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Video video){
        videoService.saveOrUpdate(video);
        return "redirect:/video/showList";
    }

    @RequestMapping("/showVideo")
    public String showVideo(@RequestParam(defaultValue = "0",required = false) int videoId, String subjectName, Model model){

        //int i= 10/0;
        Video video = videoService.getVideoById(videoId);
        int courseId = video.getCourseId();

        Course course =courseService.getCourseById(courseId);
        //List<Video> videoList = videoService.getVideoListByCourseId(courseId);

        System.out.println(course);
        model.addAttribute("course",course);
        model.addAttribute("video",video);
        model.addAttribute("subjectName",subjectName);
        return "before/section";
    }
}
